package com.yunxi.controller.sto;

import com.alibaba.fastjson.JSONObject;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.sto.pojo.VehicleOfflinePurposeVO;
import com.yunxi.sto.pojo.VehicleOfflineQueryVO;
import com.yunxi.sto.pojo.VehicleOfflineSetShareReqVO;
import com.yunxi.sto.pojo.VehicleOfflineShareReqVO;
import com.yunxi.sto.service.VehicleOfflineDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 车辆下线数据 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-05-21
 */
@RestController
@RequestMapping("/admin/vehicleOffline")
@Api(tags = "sto:vehicleOffline", description = "车辆信息管理/车辆下线数据接口")
public class VehicleOfflineController {
    private static final Logger logger = LoggerFactory.getLogger(VehicleOfflineController.class);

    @Autowired
    VehicleOfflineDubboService vehicleOfflineDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    public ApiResult page(VehicleOfflineQueryVO vehicleOfflineQueryVO, @RequestParam(required = true) Integer pageNum, @RequestParam(required = true) Integer pageSize) throws Exception {
        return ResponseUtil.ok(vehicleOfflineDubboService.findPageByVO(vehicleOfflineQueryVO, pageNum, pageSize));
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    @ApiImplicitParam(paramType = "query", name = "vehicleId", value = "主键id", required = true, dataType = "String")
    public ApiResult load(String vehicleId, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(vehicleOfflineDubboService.getDetailById(vehicleId));
    }

    @GetMapping("/vinList")
    @ApiOperation(value = "VIN码模糊查询")
    @ApiImplicitParam(paramType = "query", name = "vin", value = "VIN码", required = false, dataType = "String")
    public ApiResult load(String vin) throws Exception {
        List<String> list = vehicleOfflineDubboService.getVinList(vin);
        return ResponseUtil.ok(list);
    }

    @PostMapping("/setVehicleShareByVin")
    @ApiOperation(value = "设置共享车")
    public ApiResult setVehicleShareByVin(@RequestBody VehicleOfflineSetShareReqVO vo) {
        return ResponseUtil.ok(vehicleOfflineDubboService.setVehicleShareByVin(vo));
    }

    @GetMapping("/queryShareVehicle")
    @ApiOperation(value = "其他渠道商共享车查询")
    public ApiResult queryShareVehicle(VehicleOfflineShareReqVO vo, int pageNum, int pageSize, @CurrentUser LoginInfo user) {
        Object partnerVO = (Object) user.getPartner();
        if (partnerVO != null) {
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO), PartnerVO.class);
            vo.setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
        } else {
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401, "当前用户不是渠道商用户，请使用渠道商账户登录！");
        }

        return ResponseUtil.ok(vehicleOfflineDubboService.queryShareVehicle(vo, pageNum, pageSize));
    }

    @PostMapping("/purpose")
    @ApiOperation(value = "根据计划号设置车辆用途")
    public ApiResult vehiclePurpose(@RequestBody VehicleOfflinePurposeVO vo)throws Exception {
        vehicleOfflineDubboService.vehiclePurpose(vo);
        return ResponseUtil.ok();
    }

}
